﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Net.Sockets;
using System.Net;
using System.Xml;
using System.Xml.Serialization;

namespace ChatServer.model
{
    
    public class Room : dbconn.DBConnector
    {
        
        //name of room. retrieved by using ID in Database
        private string _roomName;
        //list of UserSockets for every User that has permissions and is active. One Socket for One User in the Room
        private List<UserSocket> _activeUserSockets;
       
        //User object of Moderator for Room
        private User _moderator;

        public Room(string _RoomName)
        {
            _roomName = _RoomName;
            _activeUserSockets = new List<UserSocket>();
        }
       
        public string RoomName
        {
            get { return _roomName; }
            set { _roomName = value; }
        }

        public List<UserSocket> ActiveUserSockets
        {
            get { return _activeUserSockets; }
            set { _activeUserSockets = value; }
        }

        //method is used to add usersocket to list of active user sockets
        public void AppendSocket(UserSocket NewUserSocket)
        {
           
            //verify userSocket is unique
            foreach (UserSocket socket in _activeUserSockets)
            {
                //compare to each user socket in the list with IPEndpoint
                if (socket.UserUserSocket.RemoteEndPoint == NewUserSocket.UserUserSocket.RemoteEndPoint)
                {
                    _activeUserSockets.Remove(socket);
                    //if ip is found in activeuserlist then we know that 
                    //no other sockets need to be removed stop searching
                    break;
                }
            }
            //Now that IP is unique, add it to the list
            _activeUserSockets.Add(NewUserSocket);
            return;
        }

        //remove user from list after they logoff
        public void RemoveUser(IPAddress endpointRemove)
        {
            foreach (UserSocket socket in _activeUserSockets)
            {
                //compare to each user socket in the list with IPaddress
                if (IPAddress.Parse(socket.UserUserSocket.RemoteEndPoint.ToString()) == endpointRemove)
                {
                    _activeUserSockets.Remove(socket);
                    //if ip is found in activeuserlist then we know that 
                    //no other sockets need to be removed stop searching
                    break;
                }
            }
        }
        //name of room
        //each user of the room
        //user ip

        //receives then forwards message to UserSocket
        public void ForwardMessage(XmlDocument doc)
        {
            //send message to each socket in active sockets
            foreach (UserSocket US in _activeUserSockets)
            {
                US.sendMessage(doc);
            }
        }
       
       
    }
}
